package com.storm.lib.listener;


import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.springframework.stereotype.Service;

import com.storm.lib.util.ExceptionUtil;
import com.storm.lib.util.FileUtil;

@Service
public class FtpServerListener {

	private static final int PORT= 2121;
	
	public void init() {
		FtpServerFactory serverFactory = new FtpServerFactory();
		ListenerFactory factory = new ListenerFactory();
		
		// set the port of the listener
		factory.setPort(PORT);

		// define SSL configuration
//		SslConfigurationFactory ssl = new SslConfigurationFactory();
//		ssl.setKeystoreFile(new File("src/test/resources/ftpserver.jks"));
//		ssl.setKeystorePassword("password");

		// set the SSL configuration for the listener
//		factory.setSslConfiguration(ssl.createSslConfiguration());
//		factory.setImplicitSsl(true);

		// replace the default listener
		serverFactory.addListener("default", factory.createListener());
		
		PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
		userManagerFactory.setFile(FileUtil.getUnderConfigFile("my-users.properties"));
		
		serverFactory.setUserManager(userManagerFactory.createUserManager());
		
		// start the server
		FtpServer server = serverFactory.createServer();
		
		try {
			server.start();
		} catch (FtpException e) {
			ExceptionUtil.processException(e);
		}
	}
	
}
